package com.shk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shk.constant.MessageConstant;
import com.shk.constant.RedisConstant;
import com.shk.entity.PageResult;
import com.shk.entity.QueryPageBean;
import com.shk.entity.Result;
import com.shk.pojo.Setmeal;
import com.shk.service.SetmealService;
import com.shk.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    /*文件上传
     * 1.获取到原始图片的文件名
     * 2.得到文件名最后一个 . 的索引位置
     * 3.根据 . 的位置分割原始文件名 得到 类似于 .jsp 的后缀名
     * 4.用uuid给图片重新命名，避免名字冲突
     * 5.上传图片到七牛云
     * 6.传给后台 图片的新名字*/

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        System.out.println(index);
        /*截取字符串*/
        String substring = originalFilename.substring(index - 1);
        System.out.println(substring);
        String fileName = UUID.randomUUID().toString() + substring;

        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            System.out.println(imgFile.getBytes());
            /*套餐未提交  图片添加到  redis 垃圾图片集合 中缓存*/
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageResult data = setmealService.findAll(currentPage, pageSize, queryString);
        return data;
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            setmealService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_sUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Setmeal data = setmealService.findById(id);
        List<Integer> ids = setmealService.findSetmealAndCheckGroup(id);
        Map map = new HashMap();
        map.put("data", data);
        map.put("ids", ids);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, map);
    }

    @RequestMapping("/edit")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds) {
        try {
            setmealService.update(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
       return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);

    }

}
